//
//  RotateViewController.m
//  XhlTrip
//
//  Created by dllo on 15/9/7.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RotateViewController.h"
#import "MBProgressHUD.h"
#import "LeaonWorking.h"
#import "UIImageView+WebCache.h"

@interface RotateViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableDictionary *resultDic;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headerView;

@end

@implementation RotateViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.resultDic = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)passDetailValue:(NSString *)idStr
{
    /* 执行它的时候别的都不执行 所以要把传过来的参数拿到他的上边 */
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    NSString *path = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/articles/", idStr, @".json"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"rotate", idStr];
    [LeaonWorking netWorkingHanderGetGataWithUrl:path pathname:nn resultBlock:^(id result) {
       
        [self.resultDic setDictionary:result];
        [_tableView reloadData];
        [self createheaderView];
        [hud removeFromSuperview];
        [hud release];
        
    }];

}


- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 210 * Height;
   
    self.headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * Height)] autorelease];
    self.tableView.tableHeaderView = self.headerView;
    
    [self.view addSubview:_tableView];
    [_tableView release];
}

- (void)createheaderView
{
    UIImageView *mage = [[UIImageView alloc] initWithFrame:self.headerView.frame];
    [mage sd_setImageWithURL:[self.resultDic objectForKey:@"image_url"]];
    [self.headerView addSubview:mage];
    [mage release];
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30 * Height)];
    name.center = self.headerView.center;
    name.text = [self.resultDic objectForKey:@"name"];
    name.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:name];
    [name release];
    name.font = [UIFont fontWithName:@"Verdana-Italic" size:26 * Width];
    name.textColor = [UIColor whiteColor];
    
    UILabel *name2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 145 * Height, self.view.frame.size.width, 30 * Height)];
    name2.font = [UIFont fontWithName:@"Verdana-Italic" size:17 * Width];
    name2.textColor = [UIColor whiteColor];
    name2.text = [self.resultDic objectForKey:@"title"];
    name2.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:name2];
    [name2 release];
    
    UILabel *ll = [[UILabel alloc] initWithFrame:CGRectMake(50 * Width, 135 * Height, self.view.frame.size.width - 100 * Width, 1.5 * Height)];
    ll.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:ll];
    [ll release];


    
}

/* 分区数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.resultDic objectForKey:@"article_sections"] count];
}


/* 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rotate"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rotate"] autorelease];
    }
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = [[[self.resultDic objectForKey:@"article_sections"] objectAtIndex:indexPath.section] objectForKey:@"description"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

/* cell 高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ss = [[[self.resultDic objectForKey:@"article_sections"] objectAtIndex:indexPath.section] objectForKey:@"description"];
    CGFloat hh = [[self class] heightWithText:ss width:self.view.frame.size.width - 20 * Width];

    return hh + 30 * Height;
    
}

+ (CGFloat)heightWithText:(NSString *)text width:(CGFloat )width
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17 * Width]} context:nil];
    return rect.size.height;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *num = [[[self.resultDic objectForKey:@"article_sections"] objectAtIndex:section] objectForKey:@"title"];
    if ([num length] > 0) {
        return num;
    }
    return nil;
}


- (void)dealloc
{
    [_headerView release];
    [_tableView reloadData];
    [_resultDic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
